package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wc.L
 * @date 2022/4/26 15:47
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    //新增菜品,同时插入菜品对应的口味数据.
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);
        Long dishId = dishDto.getId();
        //保存菜品口味数据到口味表
        List<DishFlavor> flavorList = dishDto.getFlavors();

        flavorList.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        //删除原口味表相关的数据
        LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
        dishFlavorWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorWrapper);
        //再保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void delWithFlavorByID(List<Long> ids) {
        //删除菜品之前先判断菜品有没有是在启售状态的
        //sql: select count(*) from dish where id in (1,2,3) and status = 1;
        LambdaQueryWrapper<Dish> disWrapper = new LambdaQueryWrapper<>();
        disWrapper.in(Dish::getId, ids);
        disWrapper.eq(Dish::getStatus, 1);
        int count = this.count(disWrapper);
        if (count > 0) {
            //抛出异常信息 处理
            throw new CustomException("菜品正在售卖,不能删除");
        }
        //可以删除
        this.removeByIds(ids);
        //删除菜品口味表
        LambdaQueryWrapper<DishFlavor> floaWrapper = new LambdaQueryWrapper<>();
        floaWrapper.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(floaWrapper);
    }
    //根据id查询对应的菜品信息和口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);
        return dishDto;
    }
}
